﻿using Com.OpenCode.DwgConvert.Model;
using Com.OpenCode.DwgConvert.Src.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Com.OpenCode.DwgConvert
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 数据转换模型对象
        /// </summary>
        public IDataConvert DataConvert { get; set; }

        private void bt_add_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.RestoreDirectory = true;
            openFileDialog.Filter = "DWG文件(*.dwg)|*.dwg";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                txt_bane.Text = openFileDialog.FileName;
                string input = txt_bane.Text.Trim();
                if (string.IsNullOrEmpty(input))
                {
                    throw new ArgumentNullException("输入的数据源为空");
                }
                DataConvert = new DefaultDwgToShpConvert
                {
                    InputDatasource = new TaskDatasource(input)
                };
                DataConvert.Check();
            }
        }

        private void bt_start_Click(object sender, EventArgs e)
        {
            if (DataConvert == null)
            {
                return;
            }
            DataConvert.BeforeConvert += DataConvert_BeforeConvert;
            DataConvert.Converting += DataConvert_Converting;
            DataConvert.AfterConvert += DataConvert_AfterConvert;
            Thread thread = new Thread(new ThreadStart(delegate
            {
                try
                {
                    DataConvert.Convert();
                }
                catch (Exception ex)
                {
                    string value = $"任务({DataConvert.Name})执行错误,{ex.Message}";
                    DataConvert.DataConvertLog.Add(new ErrorLogItem(value));
                    LogService.ShowError(value, ex);
                }
            }));
            thread.Start();
        }

        /// <summary>
        /// 开始转换之前事件
        /// </summary>
        /// <param name="args"></param>
        private void DataConvert_BeforeConvert(DataConvertEventArgs args)
        {
            if (args.DataConvert != null)
            {
                BeginInvoke(new Action(() =>
                {
                    pro_item.Value = 0;
                }));
            }
        }

        /// <summary>
        /// 转换进度事件
        /// </summary>
        /// <param name="args"></param>
        private void DataConvert_Converting(DataConvertProcessEventArgs args)
        {
            if (args.DataConvert != null)
            {
                BeginInvoke(new Action(() =>
                {
                    pro_item.Value = (int)args.Percent;
                }));
            }
        }

        /// <summary>
        /// 转换完成事件
        /// </summary>
        /// <param name="args"></param>
        private void DataConvert_AfterConvert(DataConvertEventArgs args)
        {
            if (args.DataConvert != null)
            {
                BeginInvoke(new Action(() =>
                {
                    pro_item.Value = 100;
                    text_log.Text = "";
                    for (int i = 0; i < DataConvert.DataConvertLog.logItems.Count; i++)
                    {
                        ILogItem logItem = DataConvert.DataConvertLog.logItems[i];
                        string info = logItem.ToString() + "\r\n";
                        text_log.Text += info;
                    }
                }));
            }
        }

        private void bt_pre_Click(object sender, EventArgs e)
        {
            if (DataConvert == null)
            {
                return;
            }
            ViewerForm viewerForm = new ViewerForm();
            viewerForm.DwgFile = DataConvert.InputDatasource.Source;
            viewerForm.ShowDialog();
        }
    }
}
